package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.GoodsDao;
import com.zoshine.zzg.dao.GoodsPublishDetailDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.Goods;
import com.zoshine.zzg.pojo.GoodsPublishDetail;
import com.zoshine.zzg.pojo.pojostatus.GoodsPublicStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**商品上架详情Service
 * @author 田沛凌
 * @time 2017/7/18 11:00
 */
@Service
@Transactional
public class GoodsPublishDetailService extends BaseService<GoodsPublishDetail> {
    private GoodsPublishDetailDao goodsPublishDetailDao;
    private GoodsDao goodsDao;
    @Autowired
    public void setGoodsPublishDetailDao(GoodsPublishDetailDao goodsPublishDetailDao) {
        this.goodsPublishDetailDao = goodsPublishDetailDao;
    }
    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }


    /**
     * 内网上架商品
     */
    public void upGoods(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.PUBLISH.getValue(), idList, new Date());
    }

    /**
     * 内网下架商品
     */
    public void downGoods(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.DOWN.getValue(), idList, new Date());
    }

    /**
     * 上架商品
     */
    @Override
    public GoodsPublishDetail add(GoodsPublishDetail goodsPublishDetail) {
        Long goodsId = goodsPublishDetail.getGoodsId();
        Goods goods = goodsDao.selectByPrimaryKey(goodsId);
        if (goods == null) {
            throw new AppException(ErrorMessage.GOODS_NOT_EXIST);
        }
        goodsPublishDetail.setGoodsId(goods.getId());
        goodsPublishDetail.setOrgId(goods.getOrgId());
        goodsPublishDetail.setBoxSpec(goods.getBoxSpec());
        goodsPublishDetail.setGoodsBarCode(goods.getGoodsBarCode());
        goodsPublishDetail.setGoodsStatus(GoodsPublicStatus.INIT.getValue());

        GoodsPublishDetail check = new GoodsPublishDetail();
        check.setGoodsId(goodsPublishDetail.getGoodsId());
        check.setGoodsStatus(goodsPublishDetail.getGoodsStatus());
        check.setPrisonsId(goodsPublishDetail.getPrisonsId());
        check = goodsPublishDetailDao.selectForCheck(check);
        if (check != null) {
            throw new AppException(ErrorMessage.GOODS_PUBLISH_PRISON_EXIST);
        }
        return super.add(goodsPublishDetail);
    }

    /**
     * 上架申请
     */
    public void upAudit(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.upAuditCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只有『待申请上架』的商品才能申请上架");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.PUBLISH_AUDIT.getValue(), idList, new Date());
    }

    /**
     * 上架通过
     */
    public void upAuditPass(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.upAuditPassCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能审核通过『上架待审核』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.PUBLISH.getValue(), idList, new Date());
    }

    /**
     * 上架不通过
     */
    public void upAuditFail(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.upAuditFailCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能审核不通过『上架待审核』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.PUBLISH_FAIL.getValue(), idList, new Date());
    }

    /**
     * 下架申请
     */
    public void downAudit(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.downAuditCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能为状态为『上架』的商品申请下架");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.DOWN_AUDIT.getValue(), idList, new Date());
    }

    /**
     * 下架通过
     */
    public void downAuditPass(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.downAuditPassCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能审核状态为『下架待审核』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.DOWN.getValue(), idList, new Date());
    }

    /**
     * 下架不通过
     */
    public void downAuditFail(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.downAuditFailCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能审核状态为『下架待审核』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.DOWN_FAIL.getValue(), idList, new Date());
    }

    /**
     * 下架
     */
    public void down(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.downCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能下架状态为『上架』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.DOWN.getValue(), idList, new Date());
    }

    /**
     * 删除上架商品，设置商品状态为过期
     */
    public void batchDel(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.delCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能删除状态为『待申请上架』『上架驳回』『上架待审核』及『下架』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.HISTORY.getValue(), idList, new Date());
    }

    /**
     * 驳回处理
     */
    public void failConfirm(String idStr) {
        String[] idArr = idStr.split(",");
        List<Long> idList = new ArrayList<>();
        for (String s : idArr) {
            Long id = Long.parseLong(s);
            idList.add(id);
        }
        if (idList.size() == 0) {
            return;
        }
        int check = goodsPublishDetailDao.failConfirmCheck(idList);
        if (check != 0) {
            throw new AppException(ErrorMessage.GOODS_STATUS_ERROR, "只能处理状态为『下架驳回』的商品");
        }
        goodsPublishDetailDao.batchUpdateStatus(GoodsPublicStatus.PUBLISH.getValue(), idList, new Date());
    }
}
